﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.Core.Extensions;
using MethodWorx.CMS.UI.Models;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Configuration;
using MethodWorx.CMS.UI.Models.Shared;
using MethodWorx.CMS.Administration;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.TemplateLanguage;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.Workflow;

namespace MethodWorx.CMS.UI.Controllers
{
    [ValidateInput(false)]
    public class ConfigurePageTemplateController : Controller
    {
        private SchemaService service;
        private ContentService content;
        private IApplicationSettingsRepository applicationSettings;
        private IPluginRepository plugins;

        public ConfigurePageTemplateController(SchemaService service, IApplicationSettingsRepository applicationSettings, IPluginRepository plugins, ContentService content)
        {
            this.service = service;
            this.applicationSettings = applicationSettings;
            this.plugins = plugins;
            this.content = content;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetIdeExtensions(Guid id)
        {
            List<object> extensions = new List<object>();

            PageTemplate template = this.service.GetPageTemplateById(id);
            if (template != null)
            {
                PluginFactory factory = new PluginFactory(this.applicationSettings, this.plugins);

                foreach (var extension in factory.GetIdeExtensionsForPageTemplate(template))
                {
                    extensions.Add(new
                    {
                        Id = extension.Id,
                        Name = extension.DisplayName
                    });
                }
            }

            return Json(extensions.ToArray(), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult BasicDetails(Guid id)
        {
            return View(new ConfigurePageTemplateModel(this.service, id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult BasicDetails(Guid id, [Bind(Prefix = "PageTemplateBasicDetails")]PageTemplateBasicDetails pageTemplateBasicDetails)
        {
            if (this.ModelState.IsValid)
            {
                this.service.UpdatePageTemplateBasicDetails(id, pageTemplateBasicDetails.Name, pageTemplateBasicDetails.Description, pageTemplateBasicDetails.Parent);
                this.ViewData["message"] = "Page Template basic details have been successfully saved";
                return View(new ConfigurePageTemplateModel(this.service, id));
            }
            else
                return View(new ConfigurePageTemplateModel(this.service, id));
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Properties(Guid id, string submit, int? index, Guid ?pageId)
        {
            var model = new ConfigurePropertiesModel(service.GetPageTemplateById(id), 0);
            if (pageId != null)
            {
                model.Templates = this.GetTemplates(content.GetPageById(pageId.Value).Template);
            }
            if (submit == "remove" && index.HasValue)
            {
                model.Remove(index.Value);
                this.service.RemovePageTemplateProperty(model.PageTemplate, index.Value);
            }
            this.ViewData["pageId"] = pageId;
            return View(model);
        }

        private IEnumerable<PageTemplate> GetTemplates(PageTemplate template)
        {
            while (template != null)
            {
                yield return template;
                template = template.Parent;
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Properties(Guid id, int? selectedPropertyIndex, PropertyDetails<object>[] propertyDetails, string submit, Guid? pageId)
        {
            var model = new ConfigurePropertiesModel(service.GetPageTemplateById(id), selectedPropertyIndex ?? 0);
            model.Properties = propertyDetails ?? new PropertyDetails<object>[] { };
            if (pageId != null)
            {
                model.Templates = this.GetTemplates(content.GetPageById(pageId.Value).Template);
            }
            switch (submit)
            {
                case "add":
                    model.Add();
                    break;
                case "save":
                    if (this.ModelState.IsValid)
                    {
                        //
                        //  ok, we are valid, so lets save
                        this.service.UpdatePageTemplateProperties(model.PageTemplate.Id, model.Properties.Cast<IPropertyDetails>());
                        this.ViewData["message"] = "Template properties have been successfully saved";
                    }
                    break;
            }
            this.ViewData["pageId"] = pageId;
            return View(model);
        }

      

   

     

    }
}
